package com.example.usermanagement.schel;

import cn.hutool.core.util.RandomUtil;
import com.example.usermanagement.liuka.model.FlowPackageUser;
import com.example.usermanagement.liuka.model.FlowUser;
import com.example.usermanagement.liuka.service.FlowPackageUserService;
import com.example.usermanagement.liuka.service.FlowUserService;
import com.example.usermanagement.publisher.ApplicationPush;
import com.example.usermanagement.publisher.ApplicationStopPush;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author yha
 * @ClassName
 * @description:TODO
 * @date 2024年09月10日
 * @version: 1.0.0
 */
@Configuration
public class RandomFlowService {

    private FlowUserService flowUserService;
    private ApplicationPush applicationPush;

    private ApplicationStopPush applicationStopPush;

    private FlowPackageUserService flowPackageUserService;



    @Autowired
    public RandomFlowService(FlowUserService flowUserService,ApplicationPush applicationPush){
        this.flowUserService = flowUserService;
        this.applicationPush = applicationPush;
    }



    @Scheduled(fixedDelay = 2000)
    public void schelFlow(){
            pushFlow();
    }

    @Scheduled(fixedDelay = 10000)
    public void schelStop(){
        List<FlowPackageUser> list = flowPackageUserService.list();
        for (FlowPackageUser flowPackageUser : list) {
            if(flowPackageUser.getEnable()==0){
                //  关闭流量卡
                applicationStopPush.pushStop(flowPackageUser.getId());
            }
        }
    }

    public void pushFlow(){
        List<FlowUser> flowUserList = flowUserService.list();
        for (FlowUser flowUser : flowUserList) {
            BigDecimal flowSize = RandomUtil.randomBigDecimal(BigDecimal.valueOf(1),BigDecimal.valueOf(50L));
            applicationPush.push(flowUser,flowSize);
        }
    }
}
